﻿using CommandProcessor.Command;
using Core.Common;
using Data.Infrastructure;
using Data.Repositories;
using Domain.Commands;
using Domain.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.Models;

namespace Domain.Handlers
{
    //Dung de check add
    public class CanAddDesignDetail : IValidationHandler<CreateOrUpdateDesignDetailCommand>
    {
        private readonly IDesignDetailRepository DesignDetailRepository;
        public CanAddDesignDetail(IDesignDetailRepository DesignDetailRepository, IUnitOfWork unitOfWork)
        {
            this.DesignDetailRepository = DesignDetailRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateDesignDetailCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.Name == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateDesignDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateDesignDetailHandler : ICommandHandler<CreateOrUpdateDesignDetailCommand>
    {
        private readonly IDesignDetailRepository DesignDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateDesignDetailHandler(IDesignDetailRepository DesignDetailRepository, IUnitOfWork unitOfWork)
        {
            this.DesignDetailRepository = DesignDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateDesignDetailCommand command)
        {
            var DesignDetail = new DesignDetail
            {
                ID = command.ID,
                Description = command.Description,
                Name = command.Name,
                Code = command.Code,
                ProductModelID=command.ProductModelID,
                Type=command.Type,
                DesignFile=command.DesignFile,
               
            };
            if (DesignDetail.ID == 0)
            {
                var xt= DesignDetailRepository.GetAll().OrderByDescending(x=>x.ID).FirstOrDefault();
                if (xt != null)
                {
                    DesignDetail.ID = xt.ID + 1;
                }
                else
                {
                    DesignDetail.ID = 1;
                }
                
                DesignDetailRepository.Add(DesignDetail);
            }
            else
            {
                var designDetail = DesignDetailRepository.GetById(DesignDetail.ID);
                if (designDetail == null)
                {
                    DesignDetailRepository.Add(DesignDetail);
                }
                else
                {
                    DesignDetailRepository.Update(DesignDetail);
                }
            }

            unitOfWork.Commit();
            return new CommandResult(true, DesignDetail.ID);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateDesignDetailCommand> commands)
        {
            List<DesignDetail> list = new List<DesignDetail>();
            foreach (var item in commands)
            {
                var DesignDetail = new DesignDetail
                {
                    ID = item.ID,
                    Description = item.Description,
                    Name = item.Name,
                    Code = item.Code,
                    ProductModelID = item.ProductModelID,
                    Type = item.Type,
                    DesignFile = item.DesignFile,

                };
                list.Add(DesignDetail);
            }
            if (list.Any(x => x.ID == 0))
                DesignDetailRepository.AddRange(list.Where(x => x.ID == 0));
            else
                DesignDetailRepository.UpdateRange(list.Where(x => x.ID != 0));
            unitOfWork.Commit();
            return new CommandResult(true);

        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateDesignDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de delete
    public class DeleteDesignDetailHandler : ICommandHandler<DeleteDesignDetailCommand>
    {
        private readonly IDesignDetailRepository DesignDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteDesignDetailHandler(IDesignDetailRepository DesignDetailRepository, IUnitOfWork unitOfWork)
        {
            this.DesignDetailRepository = DesignDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteDesignDetailCommand command)
        {
            var DesignDetail = DesignDetailRepository.GetById(command.ID);
            DesignDetailRepository.Delete(DesignDetail);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteDesignDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            DesignDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteDesignDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}

